package com.jnc.web.biz.system.controller;

import com.jnc.common.base.BaseResp;
import com.jnc.core.base.controller.BaseController;
import com.jnc.core.base.model.Role;
import com.jnc.core.base.service.RoleService;
import com.jnc.core.web.page.TableResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: jjn
 * @Date: 2018/9/18
 * @Desc:
 */
@Slf4j
@Controller
@RequestMapping("/system/role")
public class RoleController extends BaseController {
    private String prefix = "system/role";

    @Autowired
    private RoleService roleService;

    /**
     * 初始化展示页面
     * @return
     */
    @RequiresPermissions("system:role:view")
    @GetMapping()
    public String init(){
        return prefix + "/list";
    }

    /**
     * 初始化新增页面
     * @param model
     * @return
     */
    @GetMapping("/addInit")
    public String addInit(ModelMap model){
        return prefix + "/add";
    }

    /**
     * 初始化编辑页面
     * @param roleId
     * @param model
     * @return
     */
    @GetMapping("/editInit/{roleId}")
    public String editInit(@PathVariable("roleId") Integer roleId, ModelMap model){
        model.put("info", roleService.getRole(roleId));
        return prefix + "/edit";
    }

    /**
     * 查询列表数据
     * @param role
     * @return
     */
    @RequiresPermissions("system:role:list")
    @PostMapping("/list")
    @ResponseBody
    public TableResult list(Role role){
        log.debug("RoleController.list.role : {}", role);
        initPage();
        List<Role> list = roleService.queryRole(role);
        return getTableData(list);
    }

    /**
     * 新增
     * @param role
     * @return
     */
    @RequiresPermissions("system:role:add")
    @PostMapping("/add")
    @ResponseBody
    public BaseResp add(@RequestBody Role role){
        log.debug("RoleController.add.role : {}", role);
        //校验角色编码是否已存在
        List<Role> list = roleService.checkRole(role);
        if(list != null && list.size() > 0){
            return error("该角色编码已存在");
        }
        return toAjax(roleService.addRole(role));
    }

    /**
     * 编辑
     * @param role
     * @return
     */
    @RequiresPermissions("system:role:edit")
    @PostMapping("/edit")
    @ResponseBody
    public BaseResp edit(@RequestBody Role role){
        log.debug("RoleController.edit.role : {}", role);
        //校验角色编码是否已存在
        List<Role> list = roleService.checkRole(role);
        if(list != null && list.size() > 0){
            return error("该角色编码已存在");
        }
        return toAjax(roleService.editRole(role));
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @RequiresPermissions("system:role:remove")
    @PostMapping("/remove")
    @ResponseBody
    public BaseResp remove(@RequestParam String ids){
        return toAjax(roleService.deleteRole(ids));
    }


    /**
     * 新增数据权限
     */
    @GetMapping("/rule/{roleId}")
    public String rule(@PathVariable("roleId") Integer roleId, ModelMap model){
        model.put("info", roleService.getRole(roleId));
        return prefix + "/rule";
    }

    /**
     * 角色授权
     * @param role
     * @return
     */
    @RequiresPermissions("system:role:edit")
    @PostMapping("/author")
    @ResponseBody
    public BaseResp author(@RequestBody Role role){
        log.debug("RoleController.author.role : {}", role);
        return toAjax(roleService.author(role));
    }
}
